package Servlet;

import bean.User;
import net.sf.json.JSONObject;

import dao.UserDao;
import utils.JDBCUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import static com.alibaba.fastjson.JSONObject.*;

@WebServlet(name = "chat_Login", value = "/chat_Login")
public class chatLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("userName");
        userName= new String(userName.getBytes("ISO8859-1"),"UTF-8");
        String passWord = request.getParameter("passWord");

        UserDao userdao = new UserDao();
        User user = userdao.isLogin(userName,passWord);
//        System.out.println(id);
            if(user!=null){


                Map map =new HashMap();
                map.put("username",userName);
                map.put("img", user.getImg());
                map.put("id",user.getId());
//                request.setAttribute("id",user.getId());
//                System.out.println("kkk"+user.getId());
                request.setAttribute("map", JSONObject.fromObject(map));
//                request.setAttribute("img",user.getImg());

                request.getRequestDispatcher("/chat_main.jsp").forward(request,response);
            } else {
                request.setAttribute("isRegister","isfalse");

                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
